﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using ctdReader;
using ctdFunction;

namespace JsRunTool
{
    class testDllCallBack:Function
    {
        public delegate int t1();
        public static t1 te1;
        private int test1()
        {
            return 5;
        }

        public delegate int t2();
        public static t2 te2;
        private int test2()
        {
            return 10;
        }

        public delegate int t3(int i);
        public static t3 te3;
        private int test3(int i)
        {
            return i;
        }

        public delegate int t4(int i);
        public static t4 te4;
        private int test4(int i)
        {
            return i;
        }

        public delegate int t5(ref int i, [In,Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer);
        public static t5 te5;
        private int test6(ref int i, [In, Out, MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer)
        {
            string cmd = "AAAAAAAAAA";
            byte[] buf = Function.strToToHexByte(cmd);
            Array.Copy(buf, buffer, cmd.Length / 2);
            i = cmd.Length / 2;
            return i;
        }

        [DllImport("testDll.dll", EntryPoint = "add2", CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int add2(int i, int j);

        [DllImport("testDll.dll", EntryPoint = "init", CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int init(t1 i, t2 j);

        [DllImport("testDll.dll", EntryPoint = "add", CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int add(int i, int j);

        [DllImport("testDll.dll", EntryPoint = "init2", CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int init2(t3 i, t4 j);

        [DllImport("testDll.dll", EntryPoint = "add3", CharSet = CharSet.Ansi,
             CallingConvention = CallingConvention.StdCall)]
        public static extern int add3(int i, int j);

        [DllImport("testDll.dll", EntryPoint = "test5", CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int test5(UInt32 len, [MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer);

        [DllImport("testDll.dll", EntryPoint = "init5", CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int init5(int len, t5 j);

        [DllImport("testDll.dll", EntryPoint = "add5", CharSet = CharSet.Ansi,
            CallingConvention = CallingConvention.StdCall)]
        public static extern int add5(int len, [MarshalAs(UnmanagedType.LPArray, SizeConst = 300)]byte[] buffer);

        public  void test()
        {
            te1 = test1;
            te2 = test2;
            init(te1, te2);

            te3 = test3;
            te4 = test4;
            init2(te3, te4);

            int mm2 = add(1, 2);

            int mm = add3(7, 8);

            string cmd = "E7FB56F809";
            byte[] buf1 = new byte[300];
            byte[] buf = Function.strToToHexByte(cmd);
            Array.Copy(buf, buf1, cmd.Length / 2);
            UInt32 len = (UInt32)cmd.Length / 2;
            int m3 = test5(len, buf1);

            te5 = test6;
            init5(1, te5);

            int len2 = cmd.Length / 2;
            int m35 = add5(len2, buf1);

        }
    }
}
